<?php
header('Content-Type:application/json; charset=utf-8');
include("./api.inc.php");
require './config.php';
if ($_GET['act'] == 'activation') {
    $km = $_GET['code'];

    $VisitorIp = getVisitorIp();
    $row = $DB->get_row("SELECT * FROM auth_km WHERE km = '{$km}'");
    if ($km == '') {
        $arr = array("code" => 201, "msg" => "请确保所需参数无误");
        exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    }
    if (!$row) {
        $arr = array("code" => 201, "msg" => "CDK不存在！");
        exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    }
    if ($row['zt'] != '待激活') {
        $arr = array("code" => 201, "msg" => "CDK已被使用");
        exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
    }
    $DB->query("UPDATE `auth_km` SET `zt` = '已激活',`VisitorIp` = '" . $VisitorIp . "',`Expiration` = '" . time() . "', `activation` = '" . time() . "' WHERE `auth_km`.`id` = " . $row['id'] . ";");
// base64解码$km
    $str = base64_decode($km);
    $substring = strstr($str, '_'); // 获取 $delimiter 后面的部分，包括 $delimiter
    $result = str_replace("_"," ",$substring);
    $result = "item add".$result;
    $arr = array("code" => 200, "msg" => "CDK兑换成功", "command" => $result);
    exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
} else {
    $arr = array("code" => 201, "msg" => "请带入请求");
    exit(json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
?>
